/**
 * Created with Intellij IDEA.
 * Description:
 * User: Zguoxu
 * Date: 2025-02-19
 * Time: 15:55
 */
public class Test {

    public static void main(String[] args) {
        int[] array = {1, 2, 3, 4};

        //下标可以拿到
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
        System.out.println();


        //for each 循环 / 增强 for 循环
        //不依赖下标，循环中拿不到下标
        for (int x : array) {
            System.out.print(x + " ");
        }
        System.out.println();
    }
    public static void main3(String[] args) {
        //计算数组长度
        int[] array1 = {1, 2, 3, 4, 5, 6, 7};

        System.out.println(array1.length);//7
    }
    public static void main2(String[] args) {
        //既定义又初始化
        int[] array1 = {1, 2, 3, 4};

        int[] array2 = new int[10];//定义了一个大小为 10 的整型数组,并初始化0

        int[] array3 = new int[]{1, 2, 3, 4, 5, 6};//（new int[]）可以省略

        //定义+初始化
        int[] array4;
        array4 = new int[10];

        int[] array5;
        array5 = new int[]{1, 2, 3, 4, 5};

        //初始化为0+赋值
        int[] array7 = new int[10];
        array7 = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9};

        /*int[] array6;
        array6 = {1, 2, 3};*///省略就不可以分两步初始化了

        //数组变量 array 存储了数组的“地址”，哈希值，数组在堆上存储

        //引用变量：存储”地址“的变量
        System.out.println("123456789");
    }
    public static int fib2(int n) {
        //循环斐波那契
        if(n == 1) {
            return 0;
        }
        if(n == 2) {
            return 1;
        }
        int f1 = 0;
        int f2 = 1;
        int f3 = 0;
        for (int i = 3; i <= n; i++) {
            f3 = f1 + f2;
            f1 = f2;
            f2 = f3;
        }
        return f3;
    }

    public static int fib(int n) {
        //递归斐波那契
        if(n == 1) {
            return 0;
        } else if(n == 2) {
            return 1;
        } else {
            return fib(n-1) + fib(n-2);
        }
    }

    public static int func(int n) {
        //递归各位相加
        if(n < 10) {
            return n;
        } else {
            return func(n / 10) + n % 10;
        }

    }

    public static int sum(int n) {
        //递归相加
        if(n == 1) {
            return 1;
        } else {
            return n + sum(n-1);
        }
    }

    public static void print(int n) {
        //递归打印
        if(n > 10) {
            print(n / 10);
            System.out.println(n % 10);
        } else {
            System.out.println(n);
        }
    }

    public static int funFac(int n) {
        //阶乘递归
        if (n == 1) {
            return 1;
        } else {
            return n * funFac(n-1);
        }
    }

    public static void main1(String[] args) {
        System.out.println(funFac(5));
        print(12345);
        System.out.println(sum(10));
        System.out.println(func(565));
    }
}
